package com.app.community.controller;

import com.app.community.dto.CommentDTO;
import com.app.community.exception.ErrorCodeEnum;
import com.app.community.model.entity.Question;
import com.app.community.model.entity.User;
import com.app.community.service.AvatarService;
import com.app.community.service.CardService;
import com.app.community.service.CommentService;
import com.app.community.service.HomeService;
import com.wordnik.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * card页面controller
 * 2020/10/15 17:12
 **/
@Slf4j
@Controller
public class CardController {

    @Autowired
    CommentService commentService;

    @Autowired
    CardService cardService;

    @Autowired
    HomeService homeService;

    @Autowired
    AvatarService avatarService;

    @Autowired
    HttpSession httpSession;

    // 问题的id
    Long questionId = null;

    // 展示的问题
    Question question = null;

    @GetMapping(path = "card/{id}")
    public String questionCard(@PathVariable("id") Long id, Model model) {
        // 不允许未登录进入
        String userId = (String) httpSession.getAttribute("userId");
        if (userId == null || userId.isEmpty()) {
            model.addAttribute("signal", 1);
            model.addAttribute("error", ErrorCodeEnum.HAVENOTLOGIN.getDescription());
            return "common/card";
        }

        // 添加用户头像
        model.addAttribute("avatar",avatarService.selectUserAvatar(userId));

        questionId = id;
        question = cardService.selectQuestionById(id);

        // 通过creator查找user
        User user = homeService.searchUserInfo(question.getCreator());
        model.addAttribute("question_id", id);
        // 点赞数量返回给前端
        model.addAttribute("likeNumber",cardService.likeCount(questionId));

        // 获得评论以及用户信息
        List<CommentDTO> comments = commentService.selectComment(questionId);
        model.addAttribute("comments",comments);
        // 文章信心
        model.addAttribute("title", question.getTitle());
        model.addAttribute("article", question.getArticle());
        model.addAttribute("browseCount",question.getBrowseCount());

        // 评论数量
        model.addAttribute("commentNum","评论" + commentService.selectCommentNum(questionId));
        model.addAttribute("user", user);

        return "common/card";
    }

    @ApiOperation("点赞")
    @ResponseBody
    @GetMapping("likeOrDisLike")
    public String likeOrDisLike() {

        return cardService.likeOrDisLike(questionId,(String) httpSession.getAttribute("userId")).toString();
    }


    @PostMapping("comment")
    public String publishComment(@RequestParam("comment")String comment,Model model){
        String userId = (String) httpSession.getAttribute("userId");
        // 获取创建时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        // 用户、评论、日期、问题号
        commentService.publishComment(userId,comment,time,questionId);

        // 评论数量
        model.addAttribute("commentNum","评论" + commentService.selectCommentNum(questionId));
        // 获得评论以及用户信息
        List<CommentDTO> comments = commentService.selectComment(questionId);
        model.addAttribute("comments",comments);

        return "common/card::commentFragment";
    }
}
